home *** CD-ROM | disk | FTP | other *** search
- Path: news.ahc.ameritech.com!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Constructor and enum Question...
- Date: Wed, 17 Apr 1996 15:33:40 -0400
- Organization: Datalytics, Inc
- Message-ID: <31754794.1039@datalytics.com>
- References: <4kujav$892@news.nde.state.ne.us> <3173D2C2.4D68@demos.su>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Alexey Ruzin wrote:
- >
- > cwu wrote:
- > >
- > > Recently I am studying C++ and have a difficulty to understand
- > > "constructor" and "enum". I am very appreciated any comments.
- >
- > Hi, excuse me for my Engilsh.
- >
- > As for 'enum':
- >
- > In short, it looks like set of defines, which declare 'int' constants.
- > These defines are grouped in type, but you don't need use it mnemonic
- > names always. Example:
- >
- > ...
- > enum {a,b,c} f;
- > ...
- >
- > this is equal:
- >
- > ...
- > #define a 0
- > #define b 1
- > #define c 2
-
- Well, not exactly. It declares a new type, compatible with int,
- that gives names to the values. (#define creates a number which
- takes on a different type depending upon the expression in which
- the expanded value--a literal number--appears.) By default, the
- enumerations (the named values in the enumerated type) start at
- zero and increase sequentially. You can, however, assign
- arbitrary values to any of them. The compiler will increment,
- by one, any that don't have values along the way.
-
- enum
- {
- a = 5, // 5
- b, // 6
- c = 0 // 0
- };
-
- The tag-name may be omitted, as above, if you only want to use
- the enumerations and not declare a variable or parameter of the
- enumerated type. If you omit the tag-name, you are creating the
- equivalent of a set of const ints.
-
- With the tag-name, you can declare variables and parameters of
- that enumerated type, so they can only assume values from the
- enumerations in that type. You cannot, however, assign a
- variable of an enumerated type from anything other than the
- available enumerations.
-
- (Most of the preceding statements can be qualified if you start
- casting!)
-
- >
- > As for 'constructor':
- > [snip]
-
- A constructor (abbreviated ctor or c'tor) is a member function
- of a class that is charged with turning raw memory--whether on
- the stack or heap--into an object. Typically, this
- transformation involves initializing data members (dms). Prior
- to entering the body of the ctor, while processing the
- initializer list, if any, the compiler inserts code that calls
- base class and dm ctors. Thus, by the time you enter the ctor
- body (after the opening brace), all base classes and dms have
- been initialized in some fashion. From this point forward, a
- ctor is like any other member function. You can put any
- initialization code into it you like.
-
- ctors are like any other member function (mf) except that ctors
- have no return value (not even void), and they (only one per
- object) are called before any other mf. What makes ctors
- special is that they share the class name. This is how the
- compiler recognizes a particular member function as a ctor.
-
- Like other mfs, ctors may be overloaded, so each may take
- different parameters. In fact, if you don't create a ctor for
- your class, the compiler will create two for you: the default
- and copy ctors. The former takes no parameters and simply
- invokes base class and dm default ctors. The latter does a
- shallow (duplicates pointers rather than what they point to)
- copy of one object to the new one.
-
- BTW, ctors are matched by a destructor (dtor) (only one per
- class). The dtor is responsible for reverting the object back
- into raw memory. This can mean releasing resources (like memory
- allocated on the heap), or notifying cooperating objects that
- they are disappearing, etc.
-
- It comes down to this: a ctor is the first member function
- called on raw memory to turn it into an object, and the dtor is
- the last member function called on an object to turn it back
- into raw memory.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-